package leo.mystudy.sorts;

import leo.mystudy.utils.ArrayUtil;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 冒泡排序
 * @Date 2018/4/23 16:22
 * @since Ver V0.0.1
 */
public class BubbleSort {

    public static Integer[] sort(Integer[] array) {
        if (array == null || array.length <= 1) {
            return array;
        }
        for (int i = array.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (array[j] > array[j + 1]) {
                    ArrayUtil.swap(array, j, j + 1);
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        Integer[] array = new Integer[]{1, 4, 2, 5, 7, 3};
        array = sort(array);
        for (int i : array) {
            System.out.println(i);
        }
    }

}
